package com.teach;

import java.util.List;
import java.util.Scanner;

import com.teach.demo03.Machine01;
import com.teach.exception.ParamLtZeroException;

/**
 * - 控制台版本的自动售货机 模拟
 * 
 * @author J.L.Zhou
 *
 */
public class MachineConsole {

	private static Scanner sc;
	private static Machine machine = new Machine01();

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		machine.add(new Product(TypeName.Coffee, "猫屎", 20d));
		machine.add(new Product(TypeName.Coffee, "蓝山", 15d));
		machine.add(new Product(TypeName.Coffee, "蓝山", 15d));
		// add();
		mainMenu();
	}

	/**
	 * 主菜单操作
	 */
	public static void mainMenu() {
		System.out.println("------------主菜单-----------");
		System.out.println(" 0.客户菜单");
		System.out.println(" 1.管理菜单");
		System.out.println("---------------------------");
		int input = sc.nextInt();
		if (input == 0) {
			customerMenu();
		} else if (input == 1) {
			adminMenu();
		} else {
			System.out.println("输入错误");
			mainMenu();
		}
	}

	/**
	 * 顾客菜单
	 */
	public static void customerMenu() {
		System.out.println("\n\n------------顾客菜单-----------");
		System.out.println(" 0.返回主菜单");
		System.out.println(" 1.显示商品信息");
		System.out.println(" 2.投币");
		System.out.println(" 3.查看余额");
		System.out.println(" 4.找零");
		System.out.println(" 5.购买");
		System.out.println("----------------------------");
		int input = sc.nextInt();
		switch (input) {
		case 1:
			printInfo();
			break;
		case 2:
			System.out.println("请输入投币金额:");
			double money = sc.nextDouble();
			try {
				machine.addMoney(money);
			} catch (ParamLtZeroException e) {
				System.out.println(e.getMessage());
			}
			break;
		case 3:
			System.out.println("当前余额：" + machine.getMoney());
			break;
		case 4:
			System.out.println("找零：" + machine.give());
			break;
		case 5:
			buy();
			break;
		default:
			mainMenu();
			return;
		}
		customerMenu();
	}

	/**
	 * 管理菜单
	 */
	public static void adminMenu() {
		System.out.println("\n\n------------管理菜单-----------");
		System.out.println(" 0.返回主菜单");
		System.out.println(" 1.显示商品信息");
		System.out.println(" 2.添加商品");
		System.out.println(" 3.修改价格");
		System.out.println("----------------------------");
		int input = sc.nextInt();
		switch (input) {
		case 1:
			printInfo();
			break;
		case 2:
			add();
			break;
		case 3:
			modifyPrice();
			break;

		default:
			mainMenu();
			return;
		}
		adminMenu();
	}

	/**
	 * 打印商品信息
	 */
	public static void printInfo() {
		System.out.println("类别\t品牌\t价格\t数量");
		machine.list().forEach(i -> {
			System.out.print(i);
		});
		System.out.println();
	}

	/**
	 * 添加商品
	 */
	public static void add() {
		TypeName name = selectTypeName();
		System.out.println("请输入品牌:");
		String brand = sc.next();
		System.out.println("请输入价格:");
		double price = sc.nextDouble();
		machine.add(new Product(name, brand, price));
	}

	public static TypeName selectTypeName() {
		System.out.println("请选择商品类型：");
		for (TypeName n : TypeName.values()) {
			System.out.println("  " + n.ordinal() + "." + n.getTypeName());
		}
		try {
			int input = sc.nextInt();
			return TypeName.values()[input];
		} catch (Exception ex) {
			System.out.println("您选择了错误的商品类型，请重新选择");
			sc.nextLine();
			return selectTypeName();
		}
	}

	/**
	 * 购买商品
	 */
	public static void buy() {
		try {
			TypeName name = selectTypeName();
			String brand = selectBrand(name);
			// System.out.println(String.format("您选择了[%s]品牌的%s",
			// brand,name.getTypeName()));
			Product p = machine.buy(name, brand);
			System.out
					.println(String.format("您已经成功购买了[%s]品牌的%s，当前余额为%s", brand, name.getTypeName(), machine.getMoney()));
			System.out.println("具体商品：" + p);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			sc.nextLine();
		}
	}

	/**
	 * 修改价格
	 */
	public static void modifyPrice() {
		try {
			TypeName name = selectTypeName();
			String brand = selectBrand(name);
			// System.out.println(String.format("您选择了[%s]品牌的%s",
			// brand,name.getTypeName()));
			System.out.println("请输入新价格：");
			double price = sc.nextDouble();
			machine.setPrice(name, brand, price);
			System.out.println(String.format("您已经成功将[%s]品牌的%s的价格修改为%s", brand, name.getTypeName(), price));
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			sc.nextLine();
			modifyPrice();
		}
	}

	/**
	 * 选择类别下的品牌
	 * 
	 * @param name
	 * @return
	 */
	public static String selectBrand(TypeName name) {
		List<String> bs = machine.listBrand(name);
		if (bs.isEmpty()) {
			throw new RuntimeException("该类别下无任何品牌的商品");
		}
		System.out.println("请选择品牌");
		for (int i = 0; i < bs.size(); i++) {
			System.out.println(" " + i + ". " + bs.get(i));
		}
		try {
			int input = sc.nextInt();
			return bs.get(input);
		} catch (Exception ex) {
			return selectBrand(name);
		}
	}

}
